{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "577ba51a",
   "metadata": {},
   "source": [
    "---\n",
    "* 2023-10-09\n",
    "* 课程：Python语言\n",
    "* week05\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fd8d0572",
   "metadata": {},
   "source": [
    "# 本周知识点\n",
    "\n",
    "## 用户输入\n",
    "> 1. input方法介绍\n",
    "> 2. input使用\n",
    "## 注释\n",
    "> 1. 注释code的意义\n",
    "> 2. 良好的编写代码记录习惯\n",
    "> 3. 注释的一般方法\n",
    "## 课本练习\n",
    "> 1. 项目一\n",
    "> 1. 项目二\n",
    "> 1. 项目三\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "62069c77",
   "metadata": {},
   "source": [
    "# 用户输入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "08874aae",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "username:zhichao\n",
      "password:123456\n"
     ]
    }
   ],
   "source": [
    "username = input(\"username:\")\n",
    "password = input(\"password:\")\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4763269a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "zhichao 123456\n"
     ]
    }
   ],
   "source": [
    "print(username,password)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bac42745",
   "metadata": {},
   "source": [
    "## 项目练习："
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc5028c3",
   "metadata": {},
   "source": [
    "* 妙记：输入数据会存入指定变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "91bc850f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:zhichao\n",
      "age:18\n",
      "job:IT\n",
      "salary:15000\n",
      "----- INFO OF zhichao------\n",
      "age:18\n",
      "job:IT\n",
      "salary:15000\n"
     ]
    }
   ],
   "source": [
    "\n",
    "name = input(\"name:\")\n",
    "age = input(\"age:\")\n",
    "job = input(\"job:\")\n",
    "salary = input(\"salary:\")\n",
    "\n",
    "info = '''----- INFO OF ''' + name +'''------''' + '''\n",
    "age:''' + age+'''\n",
    "job:''' + job +'''\n",
    "salary:'''+salary\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "1fee719f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "----- INFO OF zhichao------\n",
      "age:18\n",
      "job:IT\n",
      "salary:15000\n"
     ]
    }
   ],
   "source": [
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0f83f979",
   "metadata": {},
   "source": [
    "* 复习：字符串的拼接可以使用 + 作为运算符，如果变量是字符串也可以与字符串相加\n",
    "> 例如上述：'''----- INFO OF ''' + name，其中name是字符串"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0b32b8ca",
   "metadata": {},
   "source": [
    "## 占位符弥补字符串与变量相加的不足"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "577b0127",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:zhichao\n",
      "age:18\n",
      "job:IT\n",
      "salary:18000\n"
     ]
    }
   ],
   "source": [
    "name = input(\"name:\")\n",
    "age = input(\"age:\")\n",
    "job = input(\"job:\")\n",
    "salary = input(\"salary:\")\n",
    "\n",
    "info = '''-------INFO OF %s -------\n",
    "Name:%s\n",
    "Age:%s\n",
    "Job:%s\n",
    "Salary:%s\n",
    "'''% (name,name,age,job,salary)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "84a1de4e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------INFO OF zhichao -------\n",
      "Name:zhichao\n",
      "Age:18\n",
      "Job:IT\n",
      "Salary:18000\n",
      "\n"
     ]
    }
   ],
   "source": [
    "print(info)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "27d77765",
   "metadata": {},
   "source": [
    "* 妙计：\n",
    "> 1. %s代表 string \n",
    "> 1. %d代表 number \n",
    "> 1. %f代表 float "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbf7a6dc",
   "metadata": {},
   "source": [
    "## 设置输入数据格式，更好的满足输入需求"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "35091b42",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name:zhichao\n",
      "age:18\n",
      "job:IT\n",
      "salary:18000\n"
     ]
    }
   ],
   "source": [
    "name = input(\"name:\")\n",
    "age = int(input(\"age:\"))  #注意！ python中默认所有的输入均为string\n",
    "job = input(\"job:\")\n",
    "salary = int(input(\"salary:\"))\n",
    "\n",
    "info = '''-------INFO OF %s -------\n",
    "Name:%s\n",
    "Age:%d\n",
    "Job:%s\n",
    "Salary:%d\n",
    "'''% (name,name,age,job,salary)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "12e52486",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------INFO OF zhichao -------\n",
      "Name:zhichao\n",
      "Age:18\n",
      "Job:IT\n",
      "Salary:18000\n",
      "\n"
     ]
    }
   ],
   "source": [
    " print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b65fba6f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weekday:Saturday\n",
      "Party!\n"
     ]
    }
   ],
   "source": [
    "today = input(\"weekday:\")\n",
    "if today == \"Saturday\":\n",
    "    print(\"Party!\")\n",
    "elif today == \"Sunday\":\n",
    "    condition = input(\"mood:\")\n",
    "    if condition == \"Headache\":\n",
    "        print(\"Recover, then rest.\")\n",
    "    else:\n",
    "        print(\"Rest.\")\n",
    "else:\n",
    "    print(\"Work, work, work.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fdef7399",
   "metadata": {},
   "source": [
    "## 猜数字游戏实践"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b6c69c57",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hide_card:········\n",
      "guess_digit:45\n",
      "think bigger...\n",
      "guess_digit:35\n",
      "think bigger...\n",
      "guess_digit:25\n",
      "think bigger...\n",
      "guess_digit:15\n",
      "think bigger...\n",
      "guess_digit:55\n",
      "yes, you got it.\n"
     ]
    }
   ],
   "source": [
    "#猜数字（0-100）\n",
    "\n",
    "\n",
    "import getpass\n",
    "hide_card = int(getpass.getpass(\"hide_card:\"))\n",
    "count = 0\n",
    "\n",
    "if hide_card <100 and hide_card >0:\n",
    "    while True:\n",
    "        if count <5:\n",
    "            guess_digit = int(input(\"guess_digit:\"))\n",
    "            if guess_digit == hide_card:                \n",
    "                print(\"yes, you got it.\")\n",
    "                break\n",
    "            elif guess_digit > hide_card:\n",
    "                print(\"think smaller...\")\n",
    "            else:\n",
    "                print(\"think bigger...\")\n",
    "            count +=1\n",
    "        else:\n",
    "            print(\"猜这么多次都不对,你个笨蛋!\")\n",
    "            print(\"正确答案：%s\"%(hide_card))\n",
    "            break\n",
    "else:\n",
    "    print(\"请输入数字在0-99之间。不要耍赖！\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "59e837cf",
   "metadata": {},
   "source": [
    "# 课本练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "92ee04c4",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
